import { toast } from "@/utils";
import { getToken } from "@/utils/auth";
import Taro from "@tarojs/taro";

export default (img, formData?) => {
  Taro.showLoading({ title: "加载中" });
  return new Promise((resolve, reject) => {
    return Taro.uploadFile({
      url: '/api/files/upload', //仅为示例，非真实的接口地址
      filePath: img,
      name: 'file',
      formData,
      header: {
        'Authorization': getToken() ?? null
      },
      success(res) {
        const { code, msg, data } = JSON.parse(res.data)
        if (code == 2) {
          reject(msg)
          toast(msg, 'error')
        } else {
          toast(msg,)
          resolve(data.absPath[0])
        }
      },
      complete: () => Taro.hideLoading()
    })
  });
}

